package su.security.module;

/**
 * 权限URL管理
 */
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.kuakao.core.dto.PagerDTO;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import su.security.entity.SecurityModule;
import su.security.entity.SecurityUrl;
import su.security.service.SecurityModuleService;
import su.security.service.SecurityUrlService;
import su.tool.SuIntUtils;
@Controller
@RequestMapping("/admin/securityUrl")
public class SecurityUrlServlet {

	@Autowired
	private SecurityUrlService securityUrlService;

	@Autowired
	private SecurityModuleService securityModuleService;

	protected final Log log = LogFactory.getLog(getClass());
	/**
	 * 首页
	 * @param request
	 * @param model
	 * @param dto
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("index")
	public String index(HttpServletRequest request, Model model,PagerDTO dto){
		//dto.setPerPage(2);
		List<SecurityUrl> urls = securityUrlService.queryByPage(dto);
		model.addAttribute("urls", urls);
		//所有模块
		List<SecurityModule> modules = securityModuleService.getAllModuleList();
		model.addAttribute("modules", modules);
		model.addAttribute("_page", dto);
		return "/maintain/security/url/url_index";
	}

	/**
	 * 到添加url的页面
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("to_add")
	public String toAdd(HttpServletRequest request, Model model){
		return "/maintain/security/url/url_edit";
	}
	
	/**
	 * 保存 
	 * @param request
	 * @param model
	 * @param url
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("do_save")
	public String doSave(HttpServletRequest request, Model model,SecurityUrl url) {
		String res = "";
		try {
			securityUrlService.save(url);
		} catch (Exception e) {
			res = e.getMessage();
		}
		if(StringUtils.isNotBlank(res)){
			request.setAttribute("errorCode", res);
			return "su_error";
		}
		return "redirect:/admin/securityUrl/index.action";
	}
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping("do_remove")
	public void doRemove(HttpServletRequest request,HttpServletResponse response, Model model){
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
        PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e1) {
			log.error(e1.getMessage());
		}
		if(writer == null)return;
		Map<String,String> msgs = new HashMap<String,String>();
		
		int id = SuIntUtils.getInt(request.getParameter("id"));
		
		if(id==0){
			msgs.put("no id", "请选择要删除的数据");
		}else{
			String msg = "";
			try {
				securityUrlService.remove(id);
			} catch (Exception e) {
				msg = e.getMessage();
			}
			if(StringUtils.isNotBlank(msg)){
				msgs.put("service error", msg);
			}else{
				msgs.put("success", "success");
			}
		}
		
		writer.print(JSONObject.fromObject(msgs));
		writer.flush();
		writer.close();
    }
	/**
	 * 到修改页面
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("to_edit")
	public String toEdit(HttpServletRequest request, Model model){
        int id = SuIntUtils.getInt(request.getParameter("id"));
        SecurityUrl url = securityUrlService.getBaseinfo(id);
        model.addAttribute("url", url);
		return "/maintain/security/url/url_edit";
	}

}
